﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

using System.Xml.XPath;
using System.Xml;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    static class ZefaniaXmlParser
    {
        private static int footnoteNumber;
        private static bool appendText;
        private static XmlNode verseNode;

        public static string bibleVerses, footnotes = "";

        //public ZefaniaXmlParser(XmlDocument XmlDoc) {
        //    bibleVerses = footnotes = "";
        //}

        public static void findVerse(XmlDocument XmlDoc, string xPath)
        {
            XmlNodeList versNodeList = null;

            try { versNodeList = XmlDoc.SelectNodes(xPath); }
            catch (XPathException) {
                bibleVerses = "Nothing found!";
                return; 
            }

            parseVerses( versNodeList );
        } // findVerse

        private static void parseVerses(XmlNodeList verseNodeList)
        {
            footnoteNumber = 1;
            bibleVerses = footnotes = "";
            try {
                foreach (XmlNode verseNode in verseNodeList) {
                    if (verseNode.HasChildNodes) 
                        foreach (XmlNode divNode in verseNode.ChildNodes)
                            parseDiv( divNode );
                    else bibleVerses += verseNode.InnerText; 

                    //richTextBox1.Font = new Font(richTextBox1.Font.FontFamily, 12.0F, FontStyle.Regular);
                    bibleVerses += verseNode.InnerText + ' '; // insert space character between verses
                } // foreach div node
            } // try
            catch (NullReferenceException) { 
                bibleVerses = "Nothing found!"; 
            }
        }

        private static void parseDiv(XmlNode divNode)
        {
            if (divNode.Name == "DIV")
            {
                //richTextBox1.Font = new Font(richTextBox1.Font.FontFamily, 8.0F, FontStyle.Underline);
                foreach (XmlNode noteNode in divNode.ChildNodes)
                {
                    Debug.WriteLine(noteNode);
                    appendText = true;
                    parseVerse();
                    footnotes += (footnoteNumber++).ToString() + ": " + noteNode.InnerText + '\n';
                }
            }
        }

        //private static void parseVerseOld(XmlNode verse, ref bool appendText)
        //{
            //foreach (char character in verse.InnerXml) {
            //    switch (character) {
            //        case '<':
            //            endDiv = true;
            //            appendText = false;
            //            break;
            //        case '>':
            //            break;
            //        case '/':
            //            appendText = true;
            //            break;
            //        default:
            //            if (appendText) {
            //                richTextBox1.AppendText(character.ToString());
            //            }
            //            break;
            //    } // switch
            //}
        //}

        private static void parseVerse()
        {
            for (int xmlVersePosition = 0; xmlVersePosition < verseNode.InnerXml.Length; xmlVersePosition++)
            {
                Debug.Write(verseNode.InnerXml[ xmlVersePosition ]);
                if (xmlVersePosition % 80 == 79) Debug.Write("\n");

                parseFootnote(ref xmlVersePosition);

                if (appendText)
                    bibleVerses += verseNode.InnerXml[ xmlVersePosition ].ToString();
            }
        }

        private static void parseFootnote(ref int xmlVersePosition)
        {
            parseFootnoteStart(ref xmlVersePosition);
            parseFootnoteEnd(ref xmlVersePosition);
        }

        //private static void parseFootnoteStartOld(XmlNode verse, ref bool appendText, ref int character)
        //{
            //if (verse.InnerXml[i]     == '<' &&
            //    verse.InnerXml[i + 1] == 'D' && 
            //    verse.InnerXml[i + 2] == 'I' &&
            //    verse.InnerXml[i + 3] == 'V' && 
            //    verse.InnerXml[i + 4] == '>' && 
            //    verse.InnerXml[i + 5] == '<' &&
            //    verse.InnerXml[i + 6] == 'N' && 
            //    verse.InnerXml[i + 7] == 'O' &&
            //    verse.InnerXml[i + 8] == 'T' && 
            //    verse.InnerXml[i + 9] == 'E' && 
            //    verse.InnerXml[i + 10] == '>' &&
        //}

        private static void parseFootnoteStart(ref int xmlVersePosition)
        {
            string footnoteStart = "<DIV><NOTE type=\"x-studynote\">";

            if (xmlVersePosition + footnoteStart.Length < verseNode.InnerXml.Length &&
                verseNode.InnerXml.Substring(xmlVersePosition, footnoteStart.Length) == footnoteStart)
            {
                appendText = false;
                
                Debug.Write("\n\n[" + (footnoteNumber++) + "] ");
                if (xmlVersePosition % 80 == 0) Debug.Write("\n");

                bibleVerses += "[" + (footnoteNumber++) + "] ";

                xmlVersePosition += footnoteStart.Length;
                //bibleVerses += "\n";
            }
        }

        //private static void parseFootnoteEndOld(XmlNode verse, ref bool appendText, string footnoteStart, string footnoteStop, ref int i)
        //{
            //if (verse.InnerXml[i] ==     '<' && 
            //    verse.InnerXml[i + 1] == '/' && 
            //    verse.InnerXml[i + 2] == 'D' &&
            //    verse.InnerXml[i + 3] == 'I' && 
            //    verse.InnerXml[i + 4] == 'V' && 
            //    verse.InnerXml[i + 5] == '>')
        //}

        private static void parseFootnoteEnd(ref int xmlVersePosition)
        {
            string footnoteStop = "</NOTE></DIV>";

            if (xmlVersePosition + footnoteStop.Length < verseNode.InnerXml.Length &&
                 verseNode.InnerXml.Substring( xmlVersePosition, footnoteStop.Length) == footnoteStop )
            {
                appendText = true;
                xmlVersePosition += footnoteStop.Length;
            }
        }
    }       // class
}           // namespace
